<?php
/**
 * session handler
 * currently uses php built-in session only.
 * will be expanded to file/db based session later.
 *
 * NOTE: DEFAULT session life time is 24 mins. just keep it as it is.
 *
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.utility
 * @copyright bbqq qstufie.com
 */
class Blib_Utility_Session
{
    /**
     * name space of session
     * @str
     */
    protected $_ns = 'default';


    /**
     * constructor
     * always start a session if not exist
     * @param str $ns the namespace used in session var
     * @return void
     */
    public function __construct($ns = 'default')
    {
        // call start session
        $this->_startSession();
        // use ns
        if (!empty($ns)) {
            $this->_ns = $ns;
        }
        // register ns as the base if not exist
        if (!isset($_SESSION[$this->_ns]) || !$_SESSION[$this->_ns] instanceof Blib_Utility_Session_Container) {
            $_SESSION[$this->_ns] = new Blib_Utility_Session_Container;
        }

    }// end __construct

/*-------------------------------[ delegated functions ]-------------------------------*/

    /**
     * set variable
     * @param str $key the key
     * @param mixed $val any value
     * @return self;
     */
    public function set($key, $val)
    {
        $_SESSION[$this->_ns]->set($key, $val);
        return $this;

    }// end set


    /**
     * unset variable
     * @param str $key the key
     * @return self;
     */
    public function clear($key = null)
    {
        $_SESSION[$this->_ns]->clear($key);
        return $this;

    }// end clear


    /**
     * get a value by key
     * @param str $key the key
     * @param mixed $default default value
     * @return mixed
     */
    public function get($key, $default = null)
    {
        return $_SESSION[$this->_ns]->get($key, $default);

    }// end get


    /**
     * start session
     * @return void
     */
    protected function _startSession()
    {
        // only start when current session is invalid
        $name    = session_name();
        $newname = md5('-main-session-pyol2-' . date('ymd'));
        if (!isset($_SESSION) || $name != $newname) {
            $this->_close();
            session_name($newname);
            session_start();
            return;
        }

    }// end _startSession


    /**
     * close session
     * @return void
     */
    protected function _close()
    {
        if (isset($_SESSION)) {
            session_unset();
            session_destroy();
        }

    }// end _close

}// end Blib_Utility_Session


/**
 * container class
 * this one deals with storing and retrieving data, as well as destroying data
 * a bit lousy to have 2 classes in one file but this is just faster...
 */
class Blib_Utility_Session_Container
{
    /**
     * the data array
     * @var array
     */
    protected $_data = array();


    /**
     * set variable
     * @param str $key the key
     * @param mixed $val any value
     * @return self;
     */
    public function set($key, $val)
    {
        $this->_data[$key] = $val;
        return $this;

    }// end set


    /**
     * unset variable
     * @param str $key the key
     * @return self;
     */
    public function clear($key = null)
    {
        if (empty($key)) {
            // clear all
            $this->_data = array();
            return $this;
        }

        unset($this->_data[$key]);
        return $this;

    }// end clear


    /**
     * get a value by key
     * @param str $key the key
     * @param mixed $default default value
     * @return mixed
     */
    public function get($key, $default = null)
    {
        if (isset($this->_data[$key])) {
            return $this->_data[$key];
        }

        return $default;

    }// end get

} // end Blib_Utility_Session_Container
?>